package alp.starcode.seller.service;

import alp.starcode.framework.db.entity.*;
import alp.starcode.framework.db.mapper.CommentsMapper;
import alp.starcode.framework.db.mapper.MerchandiseInfoMapper;
import alp.starcode.framework.db.mapper.OrderInfoMapper;
import alp.starcode.framework.db.mapper.StoreInfoMapper;
import alp.starcode.framework.db.vo.StoreInfoVO;
import alp.starcode.framework.utils.Paging;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class SellService {

    @Autowired
    OrderInfoMapper orderInfoMapper;

    @Autowired
    StoreInfoMapper storeInfoMapper;

    @Autowired
    MerchandiseInfoMapper merchandiseInfoMapper;

    @Autowired
    CommentsMapper commentsMapper;
    /**
     * 获取店铺信息(分页)
     * @param paging
     * @param search
     * @return
     */
    public IPage<StoreInfoVO> getStoreInfos(Paging paging,String search){
        LambdaQueryWrapper<StoreInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(search)){
            queryWrapper.and(a->a.like(StoreInfo::getStoreName,search).or().like(StoreInfo::getStoreAddress,search));
        }
        queryWrapper.orderByDesc(StoreInfo::getStoreName);
        Paging paging1 = storeInfoMapper.selectPage(paging, queryWrapper);
        List<StoreInfo> records = paging1.getRecords();
        List<StoreInfoVO> storeInfos = new ArrayList<>();
        if (records.size()>0){
            for (StoreInfo record : records) {
                StoreInfoVO storeInfoVO = new StoreInfoVO();
                BeanUtils.copyProperties(record,storeInfoVO);
                List<MerchandiseInfo> merchandiseInfoList = new ArrayList<>();
                LambdaQueryWrapper<MerchandiseInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(MerchandiseInfo::getStoreUuid,record.getStoreUuid());
                List<MerchandiseInfo> merchandiseInfos = merchandiseInfoMapper.selectList(lambdaQueryWrapper);
                if (merchandiseInfos.size()>0){
                    merchandiseInfoList.addAll(merchandiseInfos);
                }
                storeInfoVO.setMerchandiseInfoList(merchandiseInfoList);
                storeInfos.add(storeInfoVO);
            }
        }
        paging1.setRecords(storeInfos);
        return paging1;
    }

    /**
     * 获取店铺list
     * @param search
     * @return
     */
    public List<StoreInfoVO> getStoreInfoList(String search){
        LambdaQueryWrapper<StoreInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(search)){
            queryWrapper.and(a->a.like(StoreInfo::getStoreName,search).or().like(StoreInfo::getStoreAddress,search));
        }
        queryWrapper.orderByDesc(StoreInfo::getStoreName);
        List<StoreInfo> storeInfos1 = storeInfoMapper.selectList(queryWrapper);
        List<StoreInfoVO> storeInfos = new ArrayList<>();
        if (storeInfos1.size()>0){
           // List<MerchandiseInfo> merchandiseInfoList = new ArrayList<>();
            List<MerchandiseInfo> infoList = merchandiseInfoMapper.selectList(null);
            for (StoreInfo storeInfo : storeInfos1) {
                StoreInfoVO storeInfoVO = new StoreInfoVO();
                BeanUtils.copyProperties(storeInfo,storeInfoVO);
                List<MerchandiseInfo> merchandiseInfoList = new ArrayList<>();
                for (MerchandiseInfo merchandiseInfo : infoList) {
                    if (storeInfo.getStoreUuid().equals(merchandiseInfo.getStoreUuid())){
                        merchandiseInfoList.add(merchandiseInfo);
                    }
                }
                storeInfoVO.setMerchandiseInfoList(merchandiseInfoList);
                storeInfos.add(storeInfoVO);
            }
        }
        return storeInfos;
    }

    /**
     * 获取店铺下的所有商品
     * @param storeUuid
     * @return
     */
    public List<MerchandiseInfo> getMerchandiseByStoreUuid(String storeUuid){
        LambdaQueryWrapper<MerchandiseInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MerchandiseInfo::getStoreUuid,storeUuid);
        return merchandiseInfoMapper.selectList(queryWrapper);

    }

    public List<Comments> getAllComment(String storeUuid, HttpSession httpSession){
        Users users = (Users) httpSession.getAttribute("users");
        LambdaQueryWrapper<OrderInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderInfo::getStoreUuid,storeUuid);
        queryWrapper.eq(OrderInfo::getUserUuid,users.getUserUuid());
        List<OrderInfo> orderInfos = orderInfoMapper.selectList(queryWrapper);
        if (orderInfos.size()>0){
            Set<String> uuids = new HashSet<>();
            for (OrderInfo orderInfo : orderInfos) {
                uuids.add(orderInfo.getOrderUuid());
            }
            LambdaQueryWrapper<Comments> commentsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            commentsLambdaQueryWrapper.in(Comments::getOrderUuid,uuids);
            return commentsMapper.selectList(commentsLambdaQueryWrapper);
        }else {
            return new ArrayList<>();
        }
    }

}
